package com.example.im.ui.abstracts

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AlertDialog
import com.example.im.common.Enumerate
import com.example.im.common.Constants
import com.example.im.entity.EventBusData
import com.example.im.utils.ToastUtil
import com.trello.rxlifecycle2.LifecycleTransformer
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import com.example.im.manager.ActivityCollector
import com.example.im.mvp.base.BasePresenter
import com.example.im.mvp.base.BaseView
import com.example.im.ui.activity.AccessActivity
import kotlinx.android.synthetic.main.include_toolbar.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class AbstractBaseActivity<P : BasePresenter<*>> : RxAppCompatActivity(), BaseView {

    var mPresenter: P? = null

    abstract val layoutId: Int

    abstract fun initView()

    abstract fun initPresenter()

    override fun onCreate(savedInstanceState: Bundle?) {
        window.setBackgroundDrawable(null)
        super.onCreate(savedInstanceState)
        setContentView(layoutId)

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            val window = window
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//            window.statusBarColor = Color.TRANSPARENT
//            window.decorView.systemUiVisibility =
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                window.decorView.systemUiVisibility =
//                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//            }
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            val window = window
//            window.setFlags(
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//            )
//        }

        setSupportActionBar(tool_bar)

        ActivityCollector.addActivity((this))

        EventBus.getDefault().register(this)

        this.initView()
        this.initPresenter()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
//        if (ev!!.action == MotionEvent.ACTION_DOWN) {
//            val view = window.peekDecorView()
//            val imm  = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//            imm.hideSoftInputFromWindow(view.windowToken, 0)
//        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        if (mPresenter != null)
            mPresenter!!.detachView()
        ActivityCollector.removeActivity((this))
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(eventBus: EventBusData<*>) {
        if (eventBus.type == Enumerate.Handle.base_resource) {
            val dialog = AlertDialog.Builder(this)
                .setMessage("您的账号在其他设备上登录")
                .setPositiveButton("重新登录") { d, _ ->
                    d.cancel()
                    val preferences = getSharedPreferences(
                        Constants.ACCOUNT_ATTRIBUTES, Context.MODE_PRIVATE
                    )
                    preferences.edit().clear().apply()
                    startActivity(Intent(this, AccessActivity::class.java))
                    ActivityCollector.finishAll()
                }
                .setCancelable(false)
            dialog.show()
        }
    }

    override fun onSuccess() {

    }

    override fun onHint(hint: String) {
        ToastUtil.showShort((this), hint)
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return bindUntilEvent(ActivityEvent.STOP)
    }
}